package com.qf.listener;

import com.qf.constatns.RabbitMQConstatns;
import com.qf.utils.HttpUtils;
import com.qf.vo.Sms;
import com.rabbitmq.client.Channel;
import lombok.Data;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
@ConfigurationProperties(prefix = "sms") // 读取属性文件
@Data
public class SmsQueueListener {

    private String host;

    private String path;

    private String appCode;

    @RabbitListener(queues = RabbitMQConstatns.SMS_QUQUE)
    public void sendSms(Sms sms, Channel channel, Message message) {
        log.debug("发送短信:{}", sms);
        // 6.把发送的数据封装到Map中
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appCode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", sms.getPhone());
        querys.put("templateId", sms.getTemplateId());
        querys.put("value", sms.getCode());
        Map<String, String> bodys = new HashMap<String, String>();
        try {
           // HttpResponse response = HttpUtils.doPost(host, path, "POST", headers, querys, bodys);
            // System.out.println(EntityUtils.toString(response.getEntity()));
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
